<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="类别名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="检查周期" prop="cycle">
            <el-select v-model="ruleForm.cycle" clearable :loading="loading">
                <el-option v-for="(item,index) in cycle" :key="index" :label="item" :value="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="检查内容" required>
            <el-row class="mt5" :gutter="20" v-for="(item,index) in contents" :key="index">
                <el-col :span="20">
                    <div><el-input v-model="item.title" style="width: 100%;" /></div>
                </el-col>
                <el-col :span="2">
                    <el-button v-if="index == 0" type="primary" @click="insertContent" :icon="CirclePlus"></el-button>
                    <el-button v-if="index > 0" type="danger" @click="deleteContent(index)" :icon="Delete"></el-button>
                </el-col>
            </el-row>
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { CirclePlus,Delete } from '@element-plus/icons-vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface Cycle {
    title: string
}

interface RuleForm {
    id: number
    cycle:number
    title: string
    content:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const cycle = ref<Cycle>()
const contents = ref<any>([{title:''}])
const ruleForm = ref<RuleForm>({
    id: 0,
    title: '',
    cycle:0,
    content:'',
})

const onGetData = () => {
    http.get('/safe/type/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
            contents.value = JSON.parse(ruleForm.value.content)
            console.log(contents.value)
        }
        
        cycle.value = response.data.cycle
    })
}


const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const insertContent = () => {
    contents.value.push({title:''})
}

const deleteContent = (index:number) => {
    contents.value.splice(index,1)
}


const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
    cycle: [
        { required: true, message: '请选择检查周期', trigger: 'blur' },
    ],
    content: [
        { required: true, message: '请输入检查内容', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            ruleForm.value.content = JSON.stringify(contents.value)
            if(ruleForm.value.id){
                request = http.patch('/safe/type',ruleForm.value)
            }else{
                request = http.put('/safe/type',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
